package com.farm.controller.admin;

import com.farm.dto.GoodsDTO;
import com.farm.dto.GoodsPageQueryDTO;
import com.farm.entity.Goods;
import com.farm.result.PageResult;
import com.farm.result.Result;
import com.farm.service.GoodsService;
import com.farm.vo.GoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @FileName GoodsController
 * @Description
 * @Author Sword
 * @date 2025-02-24
 **/
@RestController
@RequestMapping("/admin/goods")
@Slf4j
@Api(tags = "商品相关接口")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增商品
     *
     * @param goodsDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增商品")
    @CacheEvict(cacheNames = "goodsCache",key = "#goodsDTO.categoryId")
    public Result save(@RequestBody GoodsDTO goodsDTO) {
        log.info("新增商品:{}", goodsDTO);
        goodsService.save(goodsDTO);
        return Result.success();
    }

    /**
     * 商品分页查询
     *
     * @param goodsPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("商品分页查询")
    public Result<PageResult> page(GoodsPageQueryDTO goodsPageQueryDTO) {
        log.info("商品分页查询:{}", goodsPageQueryDTO);
        PageResult pageResult = goodsService.page(goodsPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 商品起售、停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("商品起售、停售")
    @CacheEvict(cacheNames = "goodsCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("商品起售、停售:{}", status, id);
        goodsService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商品")
    public Result<GoodsVO> getById(@PathVariable Long id) {
        log.info("根据id查询商品:{}", id);
        GoodsVO goodsVO = goodsService.getById(id);
        return Result.success(goodsVO);
    }

    /**
     * 修改商品
     *
     * @param goodsDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品")
    @CacheEvict(cacheNames = "goodsCache",allEntries = true)
    public Result update(@RequestBody GoodsDTO goodsDTO) {
        log.info("修改商品:{}", goodsDTO);
        goodsService.update(goodsDTO);
        return Result.success();
    }


    /**
     * 批量删除商品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除商品")
    @CacheEvict(cacheNames = "goodsCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除商品:{}", ids);
        goodsService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据分类id查询商品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询商品")
    public Result<List<Goods>> list(Long categoryId) {
        log.info("根据分类id查询商品:{}", categoryId);
        List<Goods> goodsList = goodsService.list(categoryId);
        return Result.success(goodsList);
    }
}
